﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BCCommon;
using BCDto.Common.BankCardDetection;
using BCDto.Common.ErrorMessage;
using BCDto.Common.UserBankCard;
using BCDto.MySql;
using BCService.CommonService.UserBankCard;
using BCService.SmsService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BCTMSRESTful.Controllers.DriverApp.Wallet
{
    /// <summary>
    /// 银行卡接口
    /// </summary>
    [ApiExplorerSettings(GroupName = "DriverApp")]
    [Route("api/v1/DriverApp/[controller]")]
    public class UserBankCardController : DriverAppBaseController
    {
        #region 私有变量
        private readonly IUserBankCardService userBankCardService;
        private readonly IAliyunSmsService aliyunSmsService;

        #endregion

        #region 构造函数

        public UserBankCardController(IUserBankCardService userBankCardService, IAliyunSmsService aliyunSmsService)
        {
            this.userBankCardService = userBankCardService;
            this.aliyunSmsService = aliyunSmsService;
        }


        #endregion

        #region 公共方法

        #region 用户添加银行卡
        /// <summary>
        /// 用户添加银行卡
        /// </summary>
        /// <param name="dto">用户银行卡信息</param>
        /// <response code="200">返回用户银行卡信息是否添加成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(typeof(OkObjectResult), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult Post([FromBody]UserBankCardRequestDto dto)
        {
            if (!aliyunSmsService.ValidatePassCode(dto.Phone, dto.PassCode))
            {
                throw new ArgumentException("验证码错误");
            }
            var result = this.userBankCardService.Create(dto);
            return Ok();

        }
        #endregion

        #region 获取用户银行卡信息
        /// <summary>
        /// 获取用户银行卡信息
        /// </summary>
        /// <param name="userBankCardId">用户银行卡Id</param>
        /// <response code="200">用户扩展信息</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("{user_bank_card_id}")]
        [ProducesResponseType(typeof(UserBankCardDto), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult Get([FromRoute(Name = "user_bank_card_id")]long userBankCardId)
        {
            return Ok(this.userBankCardService.Get(userBankCardId));
        }
        #endregion

        #region 获取用户银行卡列表
        /// <summary>
        /// 获取用户银行卡列表
        /// </summary>     
        /// <param name="ascending">排序</param>
        /// <param name="pageIndex">当前页码</param>
        /// <param name="pageSize">每页数据条数</param>
        /// <response code="200">获取用户银行卡列表</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(PageableList<UserBankCardDto>), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult List(
            [FromQuery(Name = "ascending")]bool? ascending = null,
            [FromQuery(Name = "index")]int? pageIndex = 1,
            [FromQuery(Name = "size")]int? pageSize = 10)
        {
            if (this.User == null)
            {

                throw new ArgumentException("登录超时，请重新登录");


            }
            return this.Ok(this.userBankCardService.List(null, null, null, null, ascending, pageIndex.Value, pageSize.Value, this.UserInfo.UserId, null));
        }

        #endregion

        #region 校验银行卡类型
        /// <summary>
        /// 校验银行卡类型
        /// </summary>
        /// <param name="bankCardNo">银行卡号</param>
        /// <response code="200">银行卡类型信息</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpGet("[action]")]
        [ProducesResponseType(typeof(BankCardTypeInfoDto), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult DetectionBankCard(string bankCardNo)
        {
            return Ok(this.userBankCardService.DetectionBankCard(bankCardNo));
        }

        #endregion


        #region 解绑银行卡
        /// <summary>
        /// 用户解绑银行卡
        /// </summary>
        /// <param name="dto">解绑信息卡信息</param>
        /// <response code="200">返回用户银行卡信息是否解绑成功</response>
        /// <response code="400">请求参数或输入的数据不正确。</response>
        /// <response code="500">服务器发生错误。</response>
        /// <response code="501">某些功能尚未实现。</response>
        /// <returns></returns>
        [HttpDelete]
        [ProducesResponseType(typeof(OkObjectResult), 200)]
        [ProducesResponseType(typeof(ErrorMessageDto), 400)]
        [ProducesResponseType(typeof(ErrorMessageDto), 500)]
        public IActionResult UntyingBankCard([FromBody] UserBankUntyingRequestDto dto)
        {
            if (!aliyunSmsService.ValidatePassCode(dto.Phone, dto.PassCode))
            {
                throw new ArgumentException("验证码错误");
            }
            var result = this.userBankCardService.Delete(dto.UserBankCardId);
            return Ok();

        }
        #endregion


        #endregion
    }
}